import { createRouter, createWebHistory } from 'vue-router'
import VueCookies from 'vue-cookies'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import("@/views/login/login.vue")
    },
    {
      path: '/',
      name: 'Framework',
      component: () => import("@/views/framework/framework.vue"),
      children: [
        {
          path: '/',
          redirect: "/main/all"
        },
        {
          path: '/main/:category',
          name: "首页",
          meta: {
            needLogin: true,
            menuCode: "main"
          },
          component: () => import("@/views/main/main.vue")
        },
        {
          path: '/myshare',
          name: "我的分享",
          meta: {
            needLogin: true,
            menuCode: "share"
          },
          component: () => import("@/views/share/share.vue")
        },
        {
          path: '/session/message',
          name: "消息",
          meta: {
            needLogin: true,
            menuCode: "session"
          },
          component: () => import("@/views/session/message.vue")
        },
        {
          path: '/session/friend',
          name: "朋友",
          meta: {
            needLogin: true,
            menuCode: "session"
          },
          component: () => import("@/views/session/friend.vue")
        },

        {
          path: '/recycle',
          name: "回收站",
          meta: {
            needLogin: true,
            menuCode: "recycle"
          },
          component: () => import("@/views/recycle/recycle.vue")
        },
        {
          path: '/settings/sysSetting',
          name: "系统设置",
          meta: {
            needLogin: true,
            menuCode: "settings"
          },
          component: () => import("@/views/admin/sysSetting.vue")
        },
        {
          path: '/settings/userList',
          name: "用户管理",
          meta: {
            needLogin: true,
            menuCode: "settings"
          },
          component: () => import("@/views/admin/userList.vue")
        },
        {
          path: '/settings/fileList',
          name: "用户文件",
          meta: {
            needLogin: true,
            menuCode: "settings"
          },
          component: () => import("@/views/admin/fileList.vue")
        },
      ]
    },
    {
      path: '/shareCheck/:shareId',
      name: "分享校验",
      component: () => import("@/views/webshare/shareCheck.vue")
    },
    {
      path: '/share/:shareId',
      name: "分享",
      component: () => import("@/views/webshare/share.vue")
    }
  ]
})

router.beforeEach((to, from, next) => {
  const userInfo = VueCookies.get("userInfo")
  if (to.meta.needLogin != null && to.meta.needLogin && userInfo == null) {
    router.push("/login")
  }
  next();
})

export default router
